package com.fang.system.netty.handler;

import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;


/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/7/15 8:40 下午
 **/
public class MyAcceptHandler extends ChannelInboundHandlerAdapter {

    private final EventLoopGroup selector;
    private final ChannelHandler handler;

    public MyAcceptHandler(EventLoopGroup thread, ChannelHandler myInHandler) {
        this.selector = thread;
        this.handler = myInHandler;
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client registered...");
    }

    /**
     * @param ctx
     * @param msg 服务端读到客户端的msg，msg是Client
     *            客户端读到的数据，服务端读到的是client
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SocketChannel client = (SocketChannel) msg;
        // 1.响应式的 handler
        ChannelPipeline pipeline = client.pipeline();
        pipeline.addLast(handler);
        // 2.注册
        selector.register(client);
    }
}
